<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">房屋管理</span>
    <el-button @click="showDialog" type="primary"
               style="float:right;
               margin-top:13px;
               ">新建房屋</el-button>
  </div>
  <!-- 新增楼座弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose" style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="楼座名称">
            <el-input placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建成日期">
            <el-date-picker type="date"
                            placeholder="请选择"
                            style="width: 100%;"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            format="YYYY-MM-DD HH:mm:ss"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="房屋用途">
            <el-select placeholder="请选择">
              <el-option label="自主" value="10"></el-option>
              <el-option label="商用" value="20"></el-option>
              <el-option label="出租" value="30"></el-option>
              <el-option label="闲置" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单元数量">
            <el-select placeholder="请选择">
              <el-option label="1" value="1"></el-option>
              <el-option label="2" value="2"></el-option>
              <el-option label="3" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单元楼层数量">
            <el-select placeholder="请选择">
              <el-option label="1" value="1"></el-option>
              <el-option label="2" value="2"></el-option>
              <el-option label="3" value="3"></el-option>
              <el-option label="4" value="4"></el-option>
              <el-option label="5" value="5"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary">确定</el-button>
      <el-button>取消</el-button>
    </template>
  </el-dialog>
  <!-- 楼座搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="房屋状态">
        <el-select placeholder="请选择" style="width:220px;" v-model="selectHouse.hStatus">
          <el-option label="自住" value="10"></el-option>
          <el-option label="商用" value="20"></el-option>
          <el-option label="出租" value="30"></el-option>
          <el-option label="闲置" value="40"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼座名称" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="selectHouse.bName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadHouse">查询</el-button>
        <el-button @click="reSearch">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 楼座列表 -->
  <el-card style="margin:20px;">
    <el-table :data="houseArr">
      <el-table-column type="index" prop="hid" label="编号" width="80" align="center"/>
      <el-table-column prop="bname" label="楼座"align="center"/>
      <el-table-column prop="hunit" label="单元" align="center"/>
      <el-table-column prop="hnum" label="房间号" align="center"/>
      <el-table-column prop="hstatus" label="房屋状态" align="center" :formatter="statusFormatter"/>
      <el-table-column prop="huse" label="房屋用途" align="center"/>
      <el-table-column prop="harea" label="实用面积" align="center"/>
      <el-table-column prop="htype" label="户型" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small">详情</el-button>
          <el-button link type="primary" size="small" @click="showDialog(scope.row.hid)">编辑</el-button>
          <el-button link type="danger" size="small" @click="delHouse(scope.row.hid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";

const dialogTitle = ref('新增楼座');
const dialogVisible = ref(false);

const handleClose = ()=>{
  if (confirm('是否关闭该弹窗?')){
    dialogVisible.value = false;
    saveHouse.value = {};
  }
}

//保存新增楼座弹窗内容
// const saveHouse = ref({
//   bname: '',
//   bcreatTime: '',
//   buse: '',
//   bunitNum: '',
//   bfloorNum: ''
// })

//房屋用途值
const statusFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 10){
    cellValue = "自住";
  }else if (cellValue == 20){
    cellValue = "商用";
  }else if (cellValue == 30){
    cellValue = "出租";
  }else if (cellValue == 40){
    cellValue = "闲置";
  }
  return cellValue;
}

//保存房屋列表数据
const houseArr = ref([]);

//保存\更新房屋信息
const saveHouse = ()=>{
  let data = qs.stringify(saveHouse.value)
  axios.post(BASE_URL + '/v1/house/save', data)
      .then((response)=>{
        if (response.data.code == 2000){
          saveHouse().value = {};
          dialogVisible.value = false;
          ElMessage.success('保存成功');
          loadHouse();
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

//保存搜索条件
const selectHouse = ref({
  hStatus: '',
  bName: ''
})

//一进入页面显示楼座信息
onMounted(()=>{
  loadHouse();
})

//查询房屋
const loadHouse = ()=>{
  let data = qs.stringify(selectHouse.value);
  console.log(data);
  axios.get(BASE_URL + '/v1/house/select?' + data)
      .then((response)=>{
        if (response.data.code == 2000){
          houseArr.value = response.data.data;
          console.log(houseArr.value);
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

//重置
const reSearch = ()=>{
  selectHouse.value = {};
  loadHouse();
}

//编辑楼座信息
const editHouse = (hid)=>{
  console.log(hid);
  dialogVisible.value = true;
  dialogTitle.value = '编辑楼座';
  axios.get(BASE_URL + '/v1/house/select?hId=' + hid)
      .then((response)=>{
        if (response.data.code == 2000){
          saveHouse.value = response.data.data[0];
          console.log(saveHouse.value);
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}

//跳转到编辑页面
const showDialog = (hid)=>{
  //console.log(hid);
  router.push('/houseDialog?hid=' + hid);
}

//删除楼座信息
const delHouse = (hid)=>{
  if (confirm('确认是否删除?')) {
    axios.post(BASE_URL + '/v1/house/delete/' + hid)
        .then((response) => {
          if (response.data.code == 2000) {
            ElMessage.success('删除成功');
            loadHouse();
          } else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}
</script>

<style scoped>

</style>